package com.chenyi.data.structure.stack;

/**
 * @author by chenyi
 * @className CalculatorStack
 * @date 2022/1/15 16:12
 */

public class CalculatorStack<E> {

    /**
     * 数组模拟栈
     */
    private Object[] arr;

    /**
     * 最大容量
     */
    private int maxSize;

    /**
     * 栈顶
     */
    private int top = -1;

    private final static char ADDITION = '+';

    private final static char SUBTRACTION = '-';

    private final static char MULTIPLICATION = '*';

    private final static char DIVISION = '/';

    private final static char LEFT_BRACKETS = '(';

    private final static char RIGHT_BRACKETS = ')';

    public CalculatorStack(int maxSize) {
        this.maxSize = maxSize;
        arr = new Object[this.maxSize];
    }

    /**
     * 入栈
     *
     * @param e
     * @return
     */
    public boolean push(E e) {
        if (isFull()) {
            System.out.println("栈已满");
            return false;
        }
        arr[++top] = e;
        return true;
    }

    /**
     * 出栈
     *
     * @return
     */
    public E pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return null;
        }
        return (E) arr[top--];
    }

    /**
     * 查看栈中最后一个数据
     *
     * @return
     */
    public E peekLast() {
        if (isEmpty()) {
            System.out.println("栈为空");
            return null;
        }
        return (E) arr[top];
    }

    /**
     * 查看栈是否已满
     *
     * @return
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 查看栈是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 运算符比较 + - * /
     *
     * @param operator
     * @return
     */
    public static int operatorSize(char operator) {
        if (operator == MULTIPLICATION || operator == DIVISION) {
            return 1;
        } else if (operator == SUBTRACTION || operator == ADDITION) {
            return 0;
        } else {
            return -1;
        }
    }

    public static int operatorSize(String operator) {
        if (operator.length() > 1) {
            throw new RuntimeException("当前字符过长");
        }
        char c = operator.charAt(0);
        return operatorSize(c);
    }

    /**
     * 判断是否为符号
     * @param operator
     * @return
     */
    public static boolean isOperator(char operator) {
        return operator == MULTIPLICATION || operator == DIVISION || operator == SUBTRACTION || operator == ADDITION;
    }

    public static boolean isOperator(String operator) {
        if (operator.length() > 1) {
            throw new RuntimeException("当前字符过长");
        }
        char c = operator.charAt(0);
        return isOperator(c);
    }

    /**
     * 计算结果
     *
     * @param num1
     * @param num2
     * @param operator
     * @return
     */
    public static int calculate(int num1, int num2, char operator) {
        int result = 0;
        switch (operator) {
            case MULTIPLICATION:
                result = num1 * num2;
                break;
            case DIVISION:
                result = num2 / num1;
                break;
            case SUBTRACTION:
                result = num2 - num1;
                break;
            case ADDITION:
                result = num1 + num2;
                break;
            default:
                throw new RuntimeException("运算符有误");
        }
        return result;
    }

    public static int isBrackets(String brackets) {
        if (brackets.length() > 1) {
            throw new RuntimeException("字符过长");
        }
        char c = brackets.charAt(0);
        if (c == LEFT_BRACKETS) {
            return 0;
        } else if (c == RIGHT_BRACKETS) {
            return 1;
        } else {
            return -1;
        }
    }
}
